

package com.itheima62.znchat;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.ui.RecognizerDialogListener;
import com.itheima62.znchat.WordBean.WS;

public class MainActivity extends Activity
{

	private SpeakListenUtils	utils;
	private ListView	lv_messes;
	
	//存放说和听的消息
	private List<MessageBean> allMessages = new ArrayList<MessageBean>();
	private MyAdapter	adapter;



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//听和说的工具类
		utils = new SpeakListenUtils(this);
		
		initView();
		
		
	}
	
	private class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return allMessages.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			//判断是否有缓存
			if (convertView == null) {
				//没有缓存
				convertView = View.inflate(getApplicationContext(), R.layout.list_item, null);
				holder = new ViewHolder();
				holder.tv_answer = (TextView) convertView.findViewById(R.id.tv_answer);
				holder.tv_asker = (TextView) convertView.findViewById(R.id.tv_asker);
				holder.ll_answer = (LinearLayout) convertView.findViewById(R.id.ll_answer);
				holder.iv_answer = (ImageView) convertView.findViewById(R.id.iv_answer);
				convertView.setTag(holder);
			} else {
				//有缓存
				holder = (ViewHolder) convertView.getTag();
			}
			
			//设置数据
			//获取当前的消息
			MessageBean messageBean = allMessages.get(position);
			
			//判断消息类型
			if (messageBean.type == MessageBean.ASKER) {
				//问
				//设置view的显示
				holder.tv_asker.setVisibility(View.VISIBLE);
				
				//答的组件的隐藏
				holder.tv_answer.setVisibility(View.GONE);
				holder.ll_answer.setVisibility(View.GONE);
				holder.iv_answer.setVisibility(View.GONE);
				//设置问的信息的显示
				holder.tv_asker.setText(messageBean.message);
			} else {
				//答
				//设置问的view的隐藏
				holder.tv_asker.setVisibility(View.GONE);
				
				//答的组件的显示
				holder.tv_answer.setVisibility(View.VISIBLE);
				//设置答的信息的显示
				holder.tv_answer.setText(messageBean.message);
				holder.ll_answer.setVisibility(View.VISIBLE);
				//是否有图片
				if (messageBean.picId == -1) {
					//没有图片
					holder.iv_answer.setVisibility(View.GONE);
				} else {
					holder.iv_answer.setVisibility(View.VISIBLE);
					//设置答的图片
					holder.iv_answer.setImageResource(messageBean.picId);
				}
			}
			return convertView;
		}
		
	}
	private class ViewHolder{
		TextView tv_asker;
		TextView tv_answer;
		ImageView iv_answer;
		LinearLayout ll_answer;
	}
	
	
	private void initView() {
		setContentView(R.layout.activity_main);
		//显示说和听的信息
		lv_messes = (ListView) findViewById(R.id.lv_messeages);
		adapter = new MyAdapter();
		lv_messes.setAdapter(adapter);
		
	}

	public void listen(View v) {
		utils.listen(new RecognizerDialogListener() {
			
			@Override
			public void onResult(RecognizerResult arg0, boolean arg1) {
				//把说的话 转成json文字
				System.out.println(arg0.getResultString() );
				//gson解析结果
				Gson gson = new Gson();
				WordBean wordBean = gson.fromJson(arg0.getResultString(), WordBean.class);
				
				StringBuilder mess = new StringBuilder();
				//把每一个汉字拼接成一句话
				for (WS ws : wordBean.ws) {
					mess.append(ws.cw.get(0).w);
				}
				
				//判断消息是不是标点符号
				if (mess.length() <= 1) {
					return;//过滤掉标点符号
				}
				//根据消息内容，动态回复消息
				
				MessageBean msg = new MessageBean();
				//把说的消息封装
				msg.message = mess + "";
				msg.type = MessageBean.ASKER;
				
				//说的消息放到容器中
				allMessages.add(msg);
				
				//智能回答
				MessageBean answer = answer(mess + "");
				allMessages.add(answer);
				
				//更新界面
				
				adapter.notifyDataSetChanged();
				
				
			}
			
			@Override
			public void onError(SpeechError arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	/**
	 * @param word
	 *    说的话
	 * @return
	 *     智能回答话
	 */
	public  MessageBean answer(String word) {
		MessageBean msg = new MessageBean();
		msg.type = MessageBean.ANSWER;
		
		if (word.contains("美女")) {
			//获取文字
			msg.message = AnswerMessages.getMvDesc();
			//获取图片
			msg.picId = AnswerMessages.getMvPic();
		}
		//
		//把说的消息封装
		
		return msg;
	}
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
